package com.yingxin.yxerp.module.permission.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.mybatisplus.PageHelper;
import com.yingxin.yx.framework.user.dto.AppPermissionDto;
import com.yingxin.yxerp.module.permission.entity.AppPermission;
import com.yingxin.yxerp.module.permission.service.IAppPermissionService;
import com.yingxin.yxerp.module.permission.vo.req.AppPermissionCreateVo;
import com.yingxin.yxerp.module.permission.vo.req.AppPermissionQueryVo;
import com.yingxin.yxerp.module.permission.vo.req.AppPermissionUpdateVo;
import com.yingxin.yxerp.module.permission.vo.req.helper.AppPermissionManagerTreeHelper;
import com.yingxin.yxerp.module.permission.vo.resp.AppPermissionRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * app功能权限 前端控制器
 * </p>
 *
 * @author matongtong
 * @since 2023-12-04
 */

@Api(tags = "app功能权限")
@RestController
@RequestMapping("/appPermission")
public class AppPermissionController {

    @Autowired
    private IAppPermissionService AppPermissionService;


    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<AppPermissionRespVo> page(@RequestBody AppPermissionQueryVo queryVo) {
        IPage<AppPermissionRespVo> page = AppPermissionService.mapper().pageVo(PageHelper.in(queryVo), queryVo);
        return PageHelper.out(page);
    }

    @ApiOperation(value = "查询列表（全部）")
    @GetMapping("/list")
    public List<AppPermissionDto> list() {
        List<AppPermissionDto> list = AppPermissionService.mapper().listAppPermissionDto();
        return AppPermissionManagerTreeHelper.build(list);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public AppPermissionDto get(@PathVariable("id") String id) {
        AppPermission permission = AppPermissionService.getById(id);
        AppPermissionDto dto = new AppPermissionDto();
        BeanUtil.copyProperties(permission, dto);
        return dto;
    }


    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid AppPermissionCreateVo createVo) {
        String parentId = createVo.getId();
        AppPermission parent;
        if ("0".equals(parentId)) {
            parent = new AppPermission();
            parent.setId("0");
            parent.setLevel("0");
        } else {
            parent = AppPermissionService.getById(parentId);
            if (parent == null) {
                throw new BizException(String.format("不存在%s", parentId));
            }
        }
        String id;
        List<AppPermission> childrenList = AppPermissionService.listByParentId(parentId);
        if (CollectionUtil.isNotEmpty(childrenList)) {
            Integer max = null;
            for (AppPermission p : childrenList) {
                if (p.getApplicationName().equals(createVo.getApplicationName())) {
                    throw new BizException(String.format("已存在%s", createVo.getApplicationName()));
                }
                int var1 = Integer.parseInt(p.getId());
                if (max == null || max < var1) {
                    max = var1;
                }
            }
            id = String.valueOf(max + 1);
        } else {
            id = parentId + "01";
        }
        AppPermission permission = new AppPermission();
        permission.setId(id);
        permission.setApplicationName(createVo.getApplicationName());
        permission.setType(createVo.getType());
        permission.setLevel(String.valueOf(Integer.valueOf(parent.getLevel()) + 1));
        permission.setParentId(parentId);
        permission.setStatus("1");
        permission.setIsShow( createVo.getIsShow() );
        permission.setSort( createVo.getSort() );
        permission.setColorClass(createVo.getColorClass());
        permission.setApplicationPath(createVo.getApplicationPath());
        permission.setIcon(createVo.getIcon());
        AppPermissionService.save(permission);
        return permission.getId();
    }


    @ApiOperation(value = "修改")
    @PutMapping
    public void update(@RequestBody AppPermissionUpdateVo updateVo) {
        AppPermission permission = AppPermissionService.getById(updateVo.getId());
        if (permission == null) {
            throw new BizException(String.format("不存在%s", updateVo.getApplicationName()));
        }
        permission.setId(updateVo.getId());
        permission.setApplicationName(updateVo.getApplicationName());
        permission.setType(updateVo.getType());
        permission.setLevel(updateVo.getLevel());
        permission.setParentId(updateVo.getParentId());
        permission.setStatus("1");
        permission.setIsShow(updateVo.getIsShow());
        permission.setSort( updateVo.getSort() );
        permission.setIcon(updateVo.getIcon());
        permission.setApplicationPath(updateVo.getApplicationPath());
        permission.setColorClass(updateVo.getColorClass());
        AppPermissionService.updateById(permission);
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        AppPermission entity = AppPermissionService.getById(id);
        AppPermissionService.removeById(entity);
    }

}
